<?php
/**
 * 留言管理
 *
 * @package         YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version         $Id: MessageController.class.php v1.0 2014-06-13 17:27:03 $
 */
namespace App\Controller;
class MessageController extends CommonController{
    private $model;
    public function _initialize() {
        parent::_initialize();
        $this->model = D("Message");
    }
    
    public function index() {
        $postform = $this->model->getAddField();
        $this->assign('postform', $postform);
        $this->display();
    }
    
    public function post() {
        $addTableData = $postData = I("post.", NULL, 'RemoveXSS,RemoveHtmlScript,HtmlReplace,FilterSearch');
        //var_dump($postData);die;
        //$addTableData = I("post.addtable");
        $yd_fieldshash = I("post.yd_fieldshash", "", "trim");
        $yd_fields = I("post.yd_fields", "", "trim");
        
        if(!empty($yd_fields))
        {
            if($yd_fieldshash != md5($yd_fields. C("COOKIE_ENCODE")))
            {
                $this->error("数据校验不对，程序返回！");
                exit();
            }
        }
        if(!empty($yd_fields)){
            $addonfields = explode(';', $yd_fields);
            if(is_array($addonfields))
            {
                foreach($addonfields as $v)
                {
                    if($v==''){
                        continue;
                    }
                    $vs = explode(',', $v);
                    if(!isset($addTableData[$vs[0]]))
                    {
                        $addTableData[$vs[0]] = '';
                    }
                    else if($vs[1]=='htmltext') //HTML文本特殊处理
                    {
                        $addTableData[$vs[0]] = AnalyseHtmlBody($addTableData[$vs[0]], null, $vs[1]);
                    }else{
                        if(!isset($addTableData[$vs[0]])){
                            $addTableData[$vs[0]] = '';
                        } 
                        $addTableData[$vs[0]] = GetFieldValueA($addTableData[$vs[0]], $vs[1]);
                    }
                }
            }
        }
        $postData = array_merge($postData, $addTableData);
        $message = $this->model->create($postData);
        //var_dump($message);die;
        if($this->model->add($message)){
            $this->success("提交留言成功!");
        }else{
            $this->error("提交留言失败！");
        }
    }
}
